package pl.register.android.activities.correlation.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import pl.register.android.R;
import pl.register.android.activities.correlation.logic.Correlation;
import pl.register.android.widget.listview.RefreshableAdapter;
import pl.register.android.year.Year;
import pl.register.android.year.YearType;

import java.util.ArrayList;

/**
 * @author Mateusz Strzelecki
 * @since 0.0.1
 */
public class CorrelationSpinnerAdapter extends RefreshableAdapter<Correlation> {

    public CorrelationSpinnerAdapter(Context ctx) {
        super(ctx, new ArrayList<Correlation>());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sherlock_spinner_item, null);
        }
        Correlation correlation = getItem(position);
        TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
        textView.setText(correlation.getYearGroupName());
        textView.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.text_size_table_header));

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_list_item_single_choice, null);
        }
        Correlation correlation = getItem(position);
        TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
        textView.setHeight((int) context.getResources().getDimension(R.dimen.height_list_row));
        textView.setText(correlation.getYearGroupName());

        return convertView;
    }

    public int getIndexBy(YearType yearType) {
        int position = -1;
        for (Correlation correlation : items) {
            Year year = correlation.getYear();
            if (year.getType().equals(yearType)) {
                return items.indexOf(correlation);
            }
        }
        return position;
    }

}